package com.tianchi.lexiang.deep;

import java.lang.reflect.Method;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.tianchi.lexiang.background.util.PublicUtil;
import com.tianchi.lexiang.bean.LeXiangConstants;

public class BootReceiver extends BroadcastReceiver {

	private static final String TAG = "BootRecoeiver";
	public static final  String DETECTION_ACTION="DETECTION_ACTION";

	private PublicUtil publicUtil = PublicUtil.getPulibUtil();
    
	public static final String NO="no";
	
	private WifiManager wifiManager;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Intent deteCtionIntent = new Intent();
		Log.d(TAG, "[onReceive]     intent===>" + intent.getAction());
		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			boolean hardDisk = publicUtil.detectionHardDisk();
			Log.d(TAG, "[onReceive] hardDisk==>" + hardDisk);
			deteCtionIntent.setAction(DETECTION_ACTION);
			deteCtionIntent.putExtra(LeXiangConstants.DETECTION_HARD_DISK, hardDisk);
			context.sendStickyBroadcast(deteCtionIntent);
			checkWifiApIsEnable(context);
		}
	}
	
	private void checkWifiApIsEnable(Context context){
//		SharedPreferences pref = context.getSharedPreferences(NetworkSetting.STATIC_BACKUP, Context.MODE_WORLD_READABLE);		
//		boolean res = pref.getBoolean(NetworkSetting.WIFI_AP_ENABLE,true);
		Log.d(TAG, "[checkWifiApIsEnable] wifi ap state true");
		setWifiApEnabled(context, true);
	}

	private boolean setWifiApEnabled(Context context, boolean enabled) {  
		Log.d(TAG, "[setWifiApEnabled] " + enabled);
		wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		Log.d(TAG,"[ setWifiApEnabled]....... wifiManager ===>" +  wifiManager );

        if (enabled) { 
            wifiManager.setWifiEnabled(false);  
        }  
        try {  
			Log.d(TAG, "[setWifiApEnabled]...start setWifi");
            WifiConfiguration apConfig = new WifiConfiguration();  
            apConfig.SSID = "tianchi";  
            apConfig.preSharedKey="121221321";  
            Method method = wifiManager.getClass().getMethod(  
					"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);  
			try {
				return (Boolean) method.invoke(wifiManager, apConfig, enabled);  
			} catch (Exception e) {
				Log.d(TAG, "[setWifiApEnabled]....e===>" + e.toString());
				return false;
			}
        } catch (Exception e) {  
			Log.d(TAG, "[setWifiApEnabled]...e====>" + e.toString());
            return false;  
        }  
    }
}
